package com.lingshi.shopping_manager_api.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping_common.entity.Admin;
import com.lingshi.shopping_common.result.BaseResult;
import com.lingshi.shopping_common.service.IAdminService;
import com.lingshi.shopping_manager_api.service.ILoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台管理员
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @DubboReference
    private IAdminService adminService;

    @Autowired
    private ILoginService loginService;


    /**
     * 新增
     */
    @PostMapping("/add")
    public BaseResult add(@RequestBody Admin admin) {
        //密码加密
        admin.setPassword(SaSecureUtil.md5(admin.getPassword()));
        adminService.add(admin);
        return BaseResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public BaseResult update(@RequestBody Admin admin) {
        //密码加密
        admin.setPassword(SaSecureUtil.md5(admin.getPassword()));
        adminService.update(admin);
        return BaseResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long aid) {
        adminService.delete(aid);
        return BaseResult.success();
    }

    /**
     * 根据id查询
     */
    @GetMapping("/findById")
    public BaseResult findById(Long aid) {
        Admin admin = adminService.findById(aid);
        return BaseResult.success(admin);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     */
    @GetMapping("/search")
    @SaCheckPermission("/admin/search")
    public BaseResult search(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        Page<Admin> search = adminService.search(page, size);
        return BaseResult.success(search);
    }

    /**
     * 修改管理员角色
     *
     * @param aid  管理员id
     * @param rids 角色id
     * @return 执行结果
     */
    @PutMapping("/updateRoleToAdmin")
    public BaseResult updateRoleToAdmin(Long aid, Long[] rids) {
        adminService.updateRoleToAdmin(aid,rids);
        return BaseResult.success();
    }

    @PostMapping("/login")
    public BaseResult login(String username,String password){
        return loginService.login(username,password);
    }

    @PostMapping("/logout")
    public BaseResult logout(){
        StpUtil.logout();
        return BaseResult.success();
    }

    @GetMapping("/findAll")
    public BaseResult findAll(){
        List<Admin> admins = adminService.findAll();
        return BaseResult.success(admins);
    }
}
